package com.itheima.controller.admin;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/checkitem")
@Slf4j
@Api(tags = "检查项管理")
@RequiredArgsConstructor
public class CheckItemController {
    private final CheckItemService checkItemService;

    /**
     * 添加检查项
     *
     * @param checkItem
     */
    @PostMapping("/add")
    @ApiOperation("添加检查项")
    public Result addInspectionItems(@RequestBody CheckItem checkItem) {
        log.info("添加检查项:{}", checkItem);
        Result result = new Result();
        try {
            checkItemService.addInspectionItems(checkItem);
        } catch (Exception e) {
            result.setFlag(false);
            result.setMessage(MessageConstant.ADD_CHECKITEM_FAIL);
            return result;
        }
        result.setFlag(true);
        result.setMessage(MessageConstant.ADD_CHECKITEM_SUCCESS);
        return result;
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation("检查项分页查询")
    public PageResult page(@RequestBody QueryPageBean queryPageBean) {
        log.info("检查项分页查询：{}", queryPageBean);
        return checkItemService.pageQuery(queryPageBean);
    }

    /**
     * 删除检查项
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ApiOperation("检查项删除")
    public Result deleteInspectionItems(Long id) {
        log.info("删除id是：{}", id);
        Result result = new Result();
        try {
            checkItemService.deleteInspectionItems(id);
        } catch (Exception e) {
            result.setFlag(false);
            result.setMessage(MessageConstant.DELETE_CHECKITEM_FAIL);
            return result;
        }
        result.setFlag(true);
        result.setMessage(MessageConstant.DELETE_CHECKITEM_SUCCESS);
        return result;
    }

    /**
     * 修改检查项
     *
     * @param checkItem
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("检查项修改")
    public Result updateInspectionItems(@RequestBody CheckItem checkItem) {
        log.info("修改检查项后的内容是：{}", checkItem);
        Result result = new Result();
        try {
            checkItemService.updateInspectionItems(checkItem);
        } catch (Exception e) {
            result.setFlag(false);
            result.setMessage(MessageConstant.EDIT_CHECKITEM_FAIL);
            return result;
        }
        result.setFlag(true);
        result.setMessage(MessageConstant.EDIT_CHECKITEM_SUCCESS);
        return result;
    }


    /**
     * 查询所有检查项
     *
     * @param
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询所有检查项")
    public Result findByAllInspectionItems() {
        Result result = new Result();
        try {
            List<CheckItem> items = checkItemService.findByAllInspectionItems();
            result.setData(items);
        } catch (Exception e) {
            result.setFlag(false);
            result.setMessage(MessageConstant.QUERY_CHECKITEM_FAIL);
            return result;
        }
        result.setFlag(true);
        result.setMessage(MessageConstant.QUERY_CHECKITEM_SUCCESS);
        return result;
    }
    /**
     * 根据id查询检查项
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @ApiOperation("根据id查询检查项")
    public Result findByIdInspectionItems(Long id) {
        Result result = new Result();
        try {
            List<CheckItem> items = checkItemService.findByIdInspectionItems(id);
            result.setData(items);
        } catch (Exception e) {
            result.setFlag(false);
            result.setMessage(e.getMessage());
            return result;
        }
        result.setFlag(true);
        result.setMessage(MessageConstant.QUERY_CHECKITEM_SUCCESS);
        return result;
    }
    /**
     * 根据id查询检查项
     *
     * @param checkgroupId
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    @ApiOperation("根据id查询检查项")
    public Result findCheckItemIdsByCheckGroupId(Long checkgroupId) {
        Result result = new Result();
        try {
            List<Long> ids = checkItemService.findCheckItemIdsByCheckGroupId(checkgroupId);
            result.setData(ids);
        } catch (Exception e) {
            result.setFlag(false);
            result.setMessage(e.getMessage());
            return result;
        }
        result.setFlag(true);
        result.setMessage(MessageConstant.QUERY_CHECKITEM_SUCCESS);
        return result;
    }
}


